<?php

include '../controlador/DataBaseController.php';


class clientesModelo extends DataBaseController {

    public function ingresarCliente($fila = array(), $tabla = "") {
        $insertarcliente = $this->bd->insertar($fila, $tabla);
        return $insertarcliente;
    }

    public function ingresarActivo($fila = array(), $tabla = "") {
        $insertaractivo = $this->bd->insertar($fila, $tabla);
        return $insertaractivo;
    }

    public function buscarClientePorId($id = "") {
        $datos = $this->bd->select("*", "clientes", "id = " . $id);
        return $datos;
    }

    public function buscarActivoPorIdCliente($id = "") {
        $datos = $this->bd->select("*", "activos", "id_cliente = " . $id);
        return $datos;
    }

    public function buscarClientePorIdactivo($id = "") {
        $datos = $this->bd->select("id_cliente", "activos", "id = " . $id);
        return $datos;
    }

    public function crearTabla($columna = "", $tabla = "", $where = null, $idcliente = '') {
        $columnas = $this->bd->nombreDeColumnas($tabla);
        $datos = $this->bd->select($columna, $tabla, $where);
        $tabla = "<input type='hidden' name='id_cliente' value='$idcliente'";
        $tabla = $tabla . "<br><br><center><table border=1>\n";
        $tabla = $tabla . "<tr>";
        $tabla = $tabla . "<td colspan='3' class='titulotabla'><center>Cliente</center></td>";
        $tabla = $tabla . "</tr>\n";
        $tabla = $tabla . "<tr>";
        $tabla = $tabla . '<td rowspan=8><img src=../files/' . $idcliente . '.jpg height=180 width=180></td>';
        $i = 0;
        foreach ($datos as $value) {
            foreach ($value as $valor) {
                $tabla = $tabla . "<tr>";
                $tabla = $tabla . "<td class='titulotabla'>$columnas[$i]</td><td class='formatd'>$valor</td>\n";
                $tabla = $tabla . "</tr>";
                $i++;
            }
            $tabla = $tabla . '<tr><td><input type="image" src="../files/delete_obj.gif" name="borrarcliente" value="' . $idcliente . '" title="Eliminar">
                <img src="../files/editor_area.gif" name="editarcliente" class="editar" title="Editar">
                </td></tr>';
        }
        $tabla = $tabla . "</table></center>\n";
        return $tabla;
    }

    public function crearTablaActivos($columna = "", $tabla = "", $where = null) {
        $columnas = $this->bd->nombreDeColumnas($tabla);
        $datos = $this->bd->select($columna, $tabla, $where);
        $tabla = "<br><center><table border=1>\n";
        $tabla = $tabla . "<tr>\n";
        $tabla = $tabla . "<td colspan='10' class='titulotabla'><center>Activos</center></td></tr><tr>";
        foreach ($columnas as $key) {
            $tabla = $tabla . "<th class='titulotabla'>$key</th>\n";
        }
        $tabla = $tabla . "</tr>\n";
        foreach ($datos as $value) {
            $tabla = $tabla . "<tr>";
            foreach ($value as $key => $valor) {
                if ($key == "id") {
                    $idactivo = $valor;
                }
                $tabla = $tabla . "<td class='formatd'>$valor</td>";
            }
            $tabla = $tabla . '<td><input type="image" src="../files/delete_obj.gif" name="borraractivo" title="Eliminar" value="' . $idactivo . '"></td>';
            $tabla = $tabla . '<td><input type="image" src="../files/editor_area.gif" class="editar" name="editaractivo" title="Editar" value="' . $idactivo . '"></td>';
            $tabla = $tabla . "</tr>";
        }
        $tabla = $tabla . "</table></center>\n";
        return $tabla;
    }

    public function borrarActivos($id = "") {
        $datos1 = $this->bd->drop("activos", $id);
        return $datos1;
    }

    /*
     * @return devuelve el estado del sql borrar cliente
     */

    public function borrarclientes($id = "") {
        $datos1 = $this->bd->drop("clientes", $id);
        return $datos1;
    }

    public function formEditarCliente($columna = "", $tabla = "", $where = null, $idcliente = '') {
        $columnas = $this->bd->nombreDeColumnas($tabla);
        $datos = $this->bd->select($columna, $tabla, $where);
        $tabla = "<form action='../controlador/clientesController.php' method='post'>";
        $tabla .= "<input type='hidden' name='idcliente' value=".$idcliente.">";
        $tabla = $tabla . "<br><br><center><table border=1>\n";
        $tabla = $tabla . "<tr>";
        $tabla = $tabla . "<td colspan='3' class='titulotabla'><center>Cliente</center></td>";
        $tabla = $tabla . "</tr>\n";
        $tabla = $tabla . "<tr>";
        $tabla = $tabla . '<td rowspan=7><img src=../files/' . $idcliente . '.jpg height=180 width=180></td>';
        $i = 0;
        $j = 0;
        foreach ($datos as $value) {
            foreach ($value as $valor) {
                if ($valor == $idcliente) {
                    $tabla = $tabla . "<td class='titulotabla'>" . $columnas[$i] . "</td><td><label for=" . $columnas[$i] . "></label><input type='text' class='formatd' name='" . $columnas[$i] . "' value='" . $valor . "'></td>\n";
                } else {
                    $i++;
                    $tabla = $tabla . "<tr>";
                    $tabla = $tabla . "<td class='titulotabla'>$columnas[$i]</td><td><label for='" . $columnas[$i] . "'></label><input type='text' name='" . $columnas[$i] . "' class='formatd'value='" . $valor . "'></td>\n";
                    $tabla = $tabla . "</tr>";
                }
                $arreglo [$j] = $valor;
                $j++;
            }
        }
        $tabla = $tabla . '<tr><label for="fotoedit"></label></td><td><input type="file" name="fotoedit" id="fotoedit"></td>';
        $tabla = $tabla . '<td><input type="image" src="../files/disk-return-black.png" name="guardarcambios" id="guardarcambios" title="Guardar">
                <img src="../files/correction_change.gif" name="volver" class="volver" alt="volver" title="Volver">
                </td></tr>';
        $tabla = $tabla . "</table></center>\n";
        $tabla = $tabla . "</form>";
        return $tabla;
        //return $arreglo;
    }

    public function guardarcambios($tabla = "", $campos = Array(), $where = "") {
        
        
        $datos1 = $this->bd->update($tabla, $campos, $where);
        return $datos1;
    }
}

?>